import { PLAY_MODE, SEARCH_KEY, PLAY_KEY } from '@/assets/js/constant'
import { shuffle } from '@/assets/js/util'
import { setStorage, removeStorage, getStorage } from '@/utils/storage'
import { processSongs } from '@/service/song.js'

/* 修改url */
export async function changeList({ commit }) {
  let favoriteList = getStorage('favoriteList', 'local') || []
  favoriteList = await processSongs(favoriteList)
  commit('setFavoriteList', favoriteList)
  setStorage('favoriteList', favoriteList, 'local')

  let playHistory = getStorage(PLAY_KEY, 'local') || []
  playHistory = await processSongs(playHistory)
  commit('setPlayHistory', playHistory)
  setStorage(PLAY_KEY, playHistory, 'local')
}

export function selectPlay({ commit }, { list, index }) {
  // 选择播放
  commit('setPlayMode', PLAY_MODE.sequence)
  commit('setSequenceList', list)
  commit('setPlayingState', true)
  commit('setFullScreen', true)
  commit('setPlaylist', list)
  commit('setCurrentIndex', index)
}

export function randomPlay({ commit }, list) {
  commit('setPlayMode', PLAY_MODE.random)
  commit('setSequenceList', list)
  commit('setPlayingState', true)
  commit('setFullScreen', true)
  commit('setPlaylist', shuffle(list))
  commit('setCurrentIndex', 0)
}

export function changeMode({ commit, state, getters }, mode) {
  const currentId = getters.currentSong.id
  if (mode === PLAY_MODE.random) {
    commit('setPlaylist', shuffle(state.sequenceList))
  } else {
    commit('setPlaylist', state.sequenceList)
  }
  const index = state.playlist.findIndex(song => {
    return song.id === currentId
  })

  commit('setCurrentIndex', index)
  commit('setPlayMode', mode)
}

export function removeSong({ commit, state }, song) {
  const sequenceList = state.sequenceList.slice()
  const playlist = state.playlist.slice()

  const sequenceIndex = findIndex(sequenceList, song)
  const playIndex = findIndex(playlist, song)
  if (sequenceIndex < 0 || playIndex < 0) {
    return
  }

  sequenceList.splice(sequenceIndex, 1)
  playlist.splice(playIndex, 1)

  let currentIndex = state.currentIndex
  if (playIndex < currentIndex || currentIndex === playlist.length) {
    currentIndex--
  }

  commit('setSequenceList', sequenceList)
  commit('setPlaylist', playlist)
  commit('setCurrentIndex', currentIndex)
  if (!playlist.length) {
    commit('setPlayingState', false)
  }
}

export function clearSongList({ commit }) {
  commit('setSequenceList', [])
  commit('setPlaylist', [])
  commit('setCurrentIndex', 0)
  commit('setPlayingState', false)
}

export function addSong({ commit, state }, song) {
  const playlist = state.playlist.slice()
  const sequenceList = state.sequenceList.slice()
  let currentIndex = state.currentIndex
  const playIndex = findIndex(playlist, song)

  if (playIndex > -1) {
    currentIndex = playIndex
  } else {
    playlist.push(song)
    currentIndex = playlist.length - 1
  }

  const sequenceIndex = findIndex(sequenceList, song)
  if (sequenceIndex === -1) {
    sequenceList.push(song)
  }

  commit('setSequenceList', sequenceList)
  commit('setPlaylist', playlist)
  commit('setCurrentIndex', currentIndex)
  commit('setPlayingState', true)
  commit('setFullScreen', true)
}

function findIndex(list, song) {
  return list.findIndex(item => {
    return item.id === song.id
  })
}

export function setSearchHistory({ commit, state }, { item, type } = {}) {
  let historyList = state.historyList.slice()
  historyList = removeItem(historyList, item)

  if (type === 'add') {
    historyList.unshift(item)
  } else if (type === 'del') {
    historyList = removeItem(historyList, item)
  } else {
    historyList = []
    removeStorage(SEARCH_KEY)
  }

  setStorage(SEARCH_KEY, historyList)
  commit('setSearchHistory', historyList)
}

const removeItem = (list, item) => {
  list.forEach((v, i) => {
    const type = Object.prototype.toString
    if (type.call(item) === '[object Object]') {
      if (v.id === item.id) {
        list.splice(i, 1)
      }
    } else if (type.call(item) === '[object String]') {
      if (v === item) {
        list.splice(i, 1)
      }
    }
  })
  return list
}

export async function setPlayHistory({ commit }, { song, type = 'add' }) {
  let playHistory = getStorage(PLAY_KEY, 'local') || []
  playHistory = await processSongs(playHistory)
  playHistory = removeItem(playHistory, song)
  if (type === 'add') {
    playHistory.unshift(song)
  }
  commit('setPlayHistory', playHistory)
  setStorage(PLAY_KEY, playHistory, 'local')
}

export async function setFavoriteList({ commit }, item) {
  let favoriteList = getStorage('favoriteList', 'local') || []
  favoriteList.forEach((v, i) => {
    if (v.id === item.id) {
      favoriteList.splice(i, 1)
    }
  })
  commit('setFavoriteList', favoriteList)
  setStorage('favoriteList', favoriteList, 'local')
}
